import exceljs from "exceljs";

function get_cell_value(row, i) {
  const cell = row.getCell(i);
  switch (cell.type) {
    case exceljs.ValueType.Number:
    case exceljs.ValueType.String:
      return cell.value;
    case exceljs.ValueType.Formula:
      return cell.value.result;
    case exceljs.ValueType.Date:
      return cell.value;
    default:
      return cell.value;
  }
}

async function read_excel_data(file_path) {
  const workbook = new exceljs.Workbook();
  await workbook.xlsx.readFile(file_path);
  const worksheet = workbook.getWorksheet(1);

  const data = [];
  worksheet.eachRow({ includeEmpty: false }, (row, rowNumber) => {
    if (rowNumber > 3) {
      const rowData = {
        序号: get_cell_value(row, 1),
        构件: get_cell_value(row, 2),
        病害类型: get_cell_value(row, 9),
        病害位置: get_cell_value(row, 4),
        程度范围: get_cell_value(row, 5),
        标度: get_cell_value(row, 11),
        照片编号: get_cell_value(row, 18),
      };

      data.push(rowData);
    }
  });
  console.log(data);
  return data;
}

async function test() {
  await read_excel_data("D:/work/病害批量录入模板(24.12.5).xlsx");
}

test();
